package com.hejiale.lease.web.app.service.impl;

import com.hejiale.lease.model.entity.LeaseAgreement;
import com.hejiale.lease.model.enums.ItemType;
import com.hejiale.lease.model.enums.LeaseStatus;
import com.hejiale.lease.web.app.mapper.*;
import com.hejiale.lease.web.app.service.LeaseAgreementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hejiale.lease.web.app.vo.agreement.AgreementDetailVo;
import com.hejiale.lease.web.app.vo.agreement.AgreementItemVo;
import com.hejiale.lease.web.app.vo.graph.GraphVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author hejiale
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Override
    public List<AgreementItemVo> listItemByPhone(String username) {
        return leaseAgreementMapper.listItemByPhone(username);
    }

    /**
     * 根据id获取租约详细信息
     * @param id
     * @return
     */
    @Override
    public AgreementDetailVo getDetailById(Long id) {
        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();

        // 获取租约信息
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);

        // 获取图片
        List<GraphVo> apartmentGraphlist = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, leaseAgreement.getApartmentId());
        List<GraphVo> roomGraphlist = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, leaseAgreement.getRoomId());

        // 获取公寓名称
        String apartmentName = apartmentInfoMapper.selectById(leaseAgreement.getApartmentId()).getName();

        // 获取房间号
        String roomNumber = roomInfoMapper.selectById(leaseAgreement.getRoomId()).getRoomNumber();

        // 获取支付方式名称
        String paymentTypeName = paymentTypeMapper.selectById(leaseAgreement.getPaymentTypeId()).getName();

        // 获取租期月数 和 单位
        Integer leaseTermMonthCount = leaseTermMapper.selectById(leaseAgreement.getLeaseTermId()).getMonthCount();
        String leaseTermUnit = leaseTermMapper.selectById(leaseAgreement.getLeaseTermId()).getUnit();

        BeanUtils.copyProperties(leaseAgreement, agreementDetailVo);

        agreementDetailVo.setApartmentName(apartmentName);
        agreementDetailVo.setRoomNumber(roomNumber);
        agreementDetailVo.setPaymentTypeName(paymentTypeName);
        agreementDetailVo.setLeaseTermMonthCount(leaseTermMonthCount);
        agreementDetailVo.setLeaseTermUnit(leaseTermUnit);
        agreementDetailVo.setApartmentGraphVoList(apartmentGraphlist);
        agreementDetailVo.setRoomGraphVoList(roomGraphlist);

        return agreementDetailVo;

    }

}




